package cn.sfrog.project.biz.admin.controller;

import cn.sfrog.common.annotation.Log;
import cn.sfrog.common.annotation.RepeatSubmit;
import cn.sfrog.common.core.controller.BaseController;
import cn.sfrog.common.core.domain.AjaxResult;
import cn.sfrog.common.core.page.TableDataInfo;
import cn.sfrog.common.enums.BusinessType;
import cn.sfrog.common.utils.poi.ExcelUtil;
import cn.sfrog.project.biz.admin.domain.vo.BizStudyProjectFormVO;
import cn.sfrog.project.biz.admin.domain.vo.BizStudyProjectQueryVO;
import cn.sfrog.project.biz.admin.domain.vo.BizStudyProjectVO;
import cn.sfrog.project.biz.admin.service.IBizStudyProjectService;
import cn.sfrog.system.domain.vo.BizStatusChangeVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;


@Api(tags = "管理后台-学研项目相关")
@RestController
@RequestMapping("/biz/project")
public class BizStudyProjectController extends BaseController {
    @Resource
    private IBizStudyProjectService bizStudyProjectService;

    /**
     * 查询学研项目列表
     */
    @ApiOperation("查询列表")
    @PreAuthorize("@ss.hasPermi('biz:project:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizStudyProjectQueryVO queryVO) {
        startPage();
        List<BizStudyProjectVO> list = bizStudyProjectService.selectBizStudyProjectList(queryVO);
        return getDataTable(list);
    }

    /**
     * 导出学研项目列表
     */
    @ApiOperation("导出")
    @PreAuthorize("@ss.hasPermi('biz:project:export')")
    @Log(title = "学研项目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizStudyProjectQueryVO query) {
        List<BizStudyProjectVO> list = bizStudyProjectService.selectBizStudyProjectList(query);
        ExcelUtil<BizStudyProjectVO> util = new ExcelUtil<BizStudyProjectVO>(BizStudyProjectVO.class);
        util.exportExcel(response, list, "学研项目数据");
    }

    /**
     * 获取学研项目详细信息
     */
    @ApiOperation("获取详情")
    @PreAuthorize("@ss.hasPermi('biz:project:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(bizStudyProjectService.selectBizStudyProjectById(id));
    }

    /**
     * 新增学研项目
     */
    @ApiOperation("新增")
    @RepeatSubmit
    @PreAuthorize("@ss.hasPermi('biz:project:add')")
    @Log(title = "学研项目", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizStudyProjectFormVO form) {
        return toAjax(bizStudyProjectService.insertBizStudyProject(form));
    }

    /**
     * 修改学研项目
     */
    @ApiOperation("修改")
    @RepeatSubmit
    @PreAuthorize("@ss.hasPermi('biz:project:edit')")
    @Log(title = "学研项目", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizStudyProjectFormVO form) {
        return toAjax(bizStudyProjectService.updateBizStudyProject(form));
    }

    /**
     * 删除学研项目
     */
    @ApiOperation("删除")
    @PreAuthorize("@ss.hasPermi('biz:project:remove')")
    @Log(title = "学研项目", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(bizStudyProjectService.deleteBizStudyProjectByIds(ids));
    }

    /**
     * 更改状态学研项目
     */
    @ApiOperation("更改状态")
    @PreAuthorize("@ss.hasPermi('biz:project:changeStatus')")
    @Log(title = "学研项目", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody @Valid BizStatusChangeVO vo) {
        return AjaxResult.success(bizStudyProjectService.changeStatus(vo));
    }

    /**
     * 取消
     * @param form
     * @return
     */
    @ApiOperation("取消")
    @PreAuthorize("@ss.hasPermi('biz:project:cancel')")
    @Log(title = "学研项目", businessType = BusinessType.UPDATE)
    @PostMapping("/cancel")
    public AjaxResult cancel(@RequestBody BizStudyProjectFormVO form){
        return toAjax(bizStudyProjectService.cancel(form));
    }

    /**
     * 审核
     * @param form
     * @return
     */
    @ApiOperation("审核")
    @PreAuthorize("@ss.hasPermi('biz:project:audit')")
    @Log(title = "学研项目", businessType = BusinessType.UPDATE)
    @PostMapping("/audit")
    public AjaxResult audit(@RequestBody BizStudyProjectFormVO form){
        return toAjax(bizStudyProjectService.audit(form));
    }


    /**
     * 结束活动
     * @param id
     * @return
     */
    @ApiOperation("结束活动")
    @PreAuthorize("@ss.hasPermi('biz:project:finish')")
    @Log(title = "学研项目", businessType = BusinessType.UPDATE)
    @PostMapping("/finish/{id}")
    public AjaxResult finish(@PathVariable("id") Long id){
        return toAjax(bizStudyProjectService.finish(id));
    }

}
